//
//  BHNavigationController.swift
//  BigHealth
//
//  Created by 蔺百好 on 2021/6/1.
//

import UIKit

class BHNavigationController: UINavigationController {

    override func viewDidLoad() {
        super.viewDidLoad()

        if self.responds(to: #selector(getter: interactivePopGestureRecognizer)) == true {
            self.interactivePopGestureRecognizer?.delegate = self
        }
    }
    
    func build(_ text: String, _ normal: String, _ select: String) {
        self.tabBarItem.title = text
        
        self.tabBarItem.image = UIImage.init(named: normal)?.withRenderingMode(.alwaysOriginal)
        self.tabBarItem.selectedImage = UIImage.init(named: select)?.withRenderingMode(.alwaysOriginal)
        
        var fontSize: CGFloat = 9.0
        
        if UIScreen.isXScreen == true {
            self.tabBarItem.titlePositionAdjustment = UIOffset(horizontal: 0, vertical: 3)
            fontSize = 11
        }
        
        let normalDic = [NSAttributedString.Key.foregroundColor:UIColor.color_97949E,NSAttributedString.Key.font:UIFont.systemFont(ofSize: fontSize, weight: .medium)]
        let selectDic = [NSAttributedString.Key.foregroundColor:UIColor.color_34CE98,NSAttributedString.Key.font:UIFont.systemFont(ofSize: fontSize, weight: .medium)]
        
        self.tabBarItem.setTitleTextAttributes(normalDic, for: .normal)
        self.tabBarItem.setTitleTextAttributes(selectDic, for: .selected)
    
    }
    
}

extension BHNavigationController: UIGestureRecognizerDelegate {
    func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        
        let vc = BHBaseViewController.currentViewController
        if vc is BHBaseViewController {
            let baseVC = vc as! BHBaseViewController
            if self.viewControllers.count <= 1 {
                return false
            }else {
                return baseVC.canPopBack
            }
        }else {
            if self.viewControllers.count <= 1 {
                return false
            }else {
                return true
            }
        }
    }
}


extension BHNavigationController {
    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        if self.viewControllers.count != 0 {
            viewController.hidesBottomBarWhenPushed = true
        }
        super.pushViewController(viewController, animated: animated)
    }
    override func popViewController(animated: Bool) -> UIViewController? {
        if self.viewControllers.count <= 2 {
            self.tabBarController?.tabBar.isHidden = false
        }
        return super.popViewController(animated: animated)
    }
    
    override func present(_ viewControllerToPresent: UIViewController, animated flag: Bool, completion: (() -> Void)? = nil) {
        viewControllerToPresent.modalPresentationStyle = .fullScreen
        super.present(viewControllerToPresent, animated: flag, completion: completion)
    }
}
